package com.tim.android.widget.toggle;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.tim.android.utils.ComputeTool;
import com.tim.android.widget.R;

public class ToggleView extends FrameLayout implements View.OnClickListener {
	
	private static final String TAG = "ToggleView";

	public static enum State {
		Checked, Unchecked
	}
	
	public interface OnToggleListener {
		public void onToggle(ToggleView toggle);
	}
	
	private ImageView mImageView;
	private Drawable mCheckedDrawable;
	private Drawable mUncheckedDrawable;
	private Drawable mHighLightDrawable;
	
	private State mState;
	private boolean mIsHighLightOnChecked;
	private OnToggleListener mOnToggleListener;
	private int mOffset;
	
	public ToggleView(Context context) {
		super(context);
		initComponent();
	}

	public ToggleView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	
	public ToggleView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Toggle);
		setCheckedDrawable(a.getDrawable(R.styleable.Toggle_checked_image));
		setUncheckedDrawable(a.getDrawable(R.styleable.Toggle_unchecked_image));
		setHighLightOnChecked(a.getBoolean(R.styleable.Toggle_highlight_on_checked, false));
		setHighLightDrawable(a.getDrawable(R.styleable.Toggle_highlight_image));
		initComponent();
		computeBounds();
		a.recycle();
	}
	
	private void computeBounds() {
		if(mUncheckedDrawable == null || mCheckedDrawable == null) {
			throw new IllegalArgumentException("ToggleView need set checked_image and unchecked_image attributes!");
		}
		final int checkedWidth = mCheckedDrawable.getIntrinsicWidth();
		final int checkedHeight = mCheckedDrawable.getIntrinsicHeight();
		final int uncheckedWidth = mUncheckedDrawable.getIntrinsicWidth();
		final int uncheckedHeight = mUncheckedDrawable.getIntrinsicHeight();
		if(checkedWidth != uncheckedWidth || checkedHeight != uncheckedHeight) {
			throw new IllegalArgumentException("Checked image size not equals unchecked image size!");
		}
	}

	private void initComponent() {
		mImageView = new ImageView(getContext());
		this.mState = State.Unchecked;
		this.mOffset = (int) ComputeTool.dip2px(getContext(), 5);
		setPadding(mOffset, mOffset, mOffset, mOffset);
		addView(mImageView);
		setOnClickListener(this);
	}
	
	public void toggle() {
		Log.i(TAG, "onToggle ..");
		showHighLight();
		if(mState == State.Checked) {
			mState = State.Unchecked;
			mImageView.setBackgroundDrawable(mUncheckedDrawable);
		}
		else if(mState == State.Unchecked) {
			mState = State.Checked;
			mImageView.setBackgroundDrawable(mCheckedDrawable);
		}
	}
	
	private void showHighLight() {
		if(mIsHighLightOnChecked && mState == State.Unchecked) {
			setBackgroundDrawable(mHighLightDrawable);
		}
		else {
			setBackgroundColor(Color.TRANSPARENT);
		}
	}

	@Override
	protected void onAttachedToWindow() {
		if(mImageView.getBackground() == null) {
			mImageView.setBackgroundDrawable(mUncheckedDrawable);
		}
		super.onAttachedToWindow();
	}

	@Override
	public void onClick(View v) {
		toggle();
		if(mOnToggleListener != null) {
			mOnToggleListener.onToggle(this);
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_UP) {
			mImageView.setSelected(false);
		}
		else if(event.getAction() == MotionEvent.ACTION_DOWN) {
			mImageView.setSelected(true);
		}
		return super.onTouchEvent(event);
	}

	public void setCheckedDrawable(int resId) {
		setCheckedDrawable(getResources().getDrawable(resId));
	}
	
	public void setCheckedDrawable(Drawable drawable) {
		this.mCheckedDrawable = drawable;
	}
	
	public void setUncheckedDrawable(int resId) {
		setUncheckedDrawable(getResources().getDrawable(resId));
	}
	
	public void setUncheckedDrawable(Drawable drawable) {
		this.mUncheckedDrawable = drawable;
	}
	
	public void setHighLightDrawable(int resId) {
		setHighLightDrawable(getResources().getDrawable(resId));
	}
	
	public void setHighLightDrawable(Drawable drawable) {
		this.mHighLightDrawable = drawable;
	}
	
	public State getState() {
		return mState;
	}
	
	public void setHighLightOnChecked(boolean isHighLight) {
		this.mIsHighLightOnChecked = isHighLight;
	}
	
	public boolean isHighLightOnChecked() {
		return mIsHighLightOnChecked;
	}
	
	public void setOnToggleListener(OnToggleListener l) {
		this.mOnToggleListener = l;
	}
}
